package com.eascs.loansettled.controller;

import java.text.DecimalFormat;
import java.text.Format;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.loansettled.entity.O2oLoanSettledFormEntity;
import com.eascs.loansettled.service.O2oLoanSettledService;
import com.eascs.notice.controller.NoticeBaseMasterController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;

/**
 * 结清确认
 * @author user
 *
 */
@Controller
@RequestMapping("/loansettled")
public class O2oLoanSettledController extends NoticeBaseMasterController {

	@Autowired
	O2oLoanSettledService loanSettledService;
	
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "Settlement_confirmation");
	}

	@RequestMapping("/index")
	public String findBankLoan(O2oLoanSettledFormEntity entity) {
		String o2ourl = "/loansettled/index"; 
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String bankLoan = entity.getBankLoan() == null ? "" : entity.getBankLoan();
			String operName = entity.getOperName() == null ? "" : entity.getOperName();
			String loanUserName = entity.getLoanUserName() == null ? "" : entity.getLoanUserName();
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String createUserName = entity.getCreateUserName() == null ? "" : entity.getCreateUserName();
			String createDateStart = entity.getCreateDateStart() == null ? null : entity.getCreateDateStart();
			String createDateEnd = entity.getCreateDateEnd() == null ? null : entity.getCreateDateEnd();
			String dateDueStart = entity.getDateDueStart() == null ? null : entity.getDateDueStart();
			String dateDueEnd = entity.getDateDueEnd() == null ? null : entity.getDateDueEnd();
			String ltdName = entity.getLtdName() == null ? "" : entity.getLtdName();
			String ltd = entity.getLtd() == null ? "" : entity.getLtd();
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			String province = entity.getProvince() == null ? "" : entity.getProvince();
			String gdStatus = entity.getGdStatus() == null ? "" : entity.getGdStatus();
			String repaystatus = entity.getRepaystatus() == null ? "" : entity.getRepaystatus();
			String idc = entity.getIdc() == null ? "" : entity.getIdc();
			String regionalName = entity.getRegionalName()== null ? "" : entity.getRegionalName();
			String brandName = entity.getBrandName()== null ? "" : entity.getBrandName();
			String partnerName = entity.getPartnerName()== null ? "" : entity.getPartnerName();
			String hostProvince = entity.getHostProvince()== null ? "" : entity.getHostProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "bankLoan", bankLoan);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operName", operName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createUserName", createUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateStart", createDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateEnd", createDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueStart", dateDueStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueEnd", dateDueEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltdName", ltdName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "gdStatus", gdStatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "repaystatus", repaystatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "idc", idc);
			o2ourl = UrlUtil.appendUrl(o2ourl, "regionalName", regionalName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "brandName", brandName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "hostProvince", hostProvince);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		PageResult<Object[]> pageResult = loanSettledService.findByPage(entity);
		String[] titles = { "贷款单号","借款人姓名","银行","平台名称","贷款类型","省区","放款金额(万元)","放款日期","到期日期","状态"};
		SelectDataOption[] selectDataOptions = { null,  null,new SelectDataOption("BANKTYPEO2O"), null, new SelectDataOption("OTO_LOAN_TYPE"),null, null,  null,null,new SelectDataOption("SQZT") };
		Button[] titleButtons = new Button[]{ButtonHelper.createAjaxButton(null, "/rloansettled/detail?loanapplyNo=")};
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles,o2ourl);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setTitleButtons(titleButtons); 
		paginationInfo.setBuildSelectBox(true);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setFormats(new Format[] { null, null, null, null,null,null,new DecimalFormat(",##0.00"), null, null });
		ViewContext.put("paginationInfo", paginationInfo);
		// ViewContext.put("oaurl", oaurl);
		ViewContext.put("vo", entity);
		return "loansettled/index";
	}
}
